from django.db import models

# Create your models here.
from rest_framework.authtoken.models import Token


class CommonFields(models.Model):
    """
    通用字段模型
    """
    code = models.CharField(max_length=20, verbose_name='编码', unique=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    remark = models.TextField(verbose_name='备注', blank=True, null=True)
    # token = models.OneToOneField(Token, on_delete=models.CASCADE)

    class Meta:
        abstract = True


class GoodsCategory(CommonFields):
    """
    商品类型模型类
    """
    # category_code = models.CharField(max_length=20, verbose_name='类型编码', unique=True)
    category_name = models.CharField(max_length=30, null=False, verbose_name='类型名称')

    class Meta:
        verbose_name = '商品类型'
        verbose_name_plural = '商品类型'

    def __str__(self):
        return self.category_name


class Units(CommonFields):
    """
    单位模型类
    """
    unit_name = models.CharField(max_length=20, null=False, verbose_name='单位')

    class Meta:
        verbose_name = '单位'
        verbose_name_plural = '单位'

    def __str__(self):
        return self.unit_name


# class Colors(CommonFields):
#     """
#     颜色模型类
#     """
#     color_name = models.CharField(max_length=30, null=False, verbose_name='颜色')
#
#     class Meta:
#         verbose_name = '颜色'
#         verbose_name_plural = '颜色'
#
#     def __str__(self):
#         return self.color_name


class Suppliers(CommonFields):
    """
    供应商模型类
    """
    name = models.CharField(max_length=100, verbose_name='供应商名称')
    address = models.CharField(max_length=200, verbose_name='供应商地址', null=True, blank=True)
    contact = models.CharField(max_length=50, verbose_name='联系人', null=True, blank=True)
    phone = models.CharField(max_length=20, verbose_name='联系电话', null=True, blank=True)
    email = models.EmailField(max_length=100, verbose_name='邮箱', null=True, blank=True)

    class Meta:
        verbose_name = '供应商'
        verbose_name_plural = '供应商'

    def __str__(self):
        return self.name


class Clients(CommonFields):
    """
    客户模型
    """
    name = models.CharField(max_length=100, verbose_name='客户名称')
    address = models.CharField(max_length=200, verbose_name='客户地址', null=True, blank=True)
    contact = models.CharField(max_length=50, verbose_name='联系人', null=True, blank=True)
    phone = models.CharField(max_length=20, verbose_name='联系电话', null=True, blank=True)
    email = models.EmailField(max_length=100, verbose_name='邮箱', null=True, blank=True)

    class Meta:
        verbose_name = '客户'
        verbose_name_plural = '客户'

    def __str__(self):
        return self.name


class Product(CommonFields):
    """
    商品模型类
    """
    goods_id = models.CharField(max_length=50, verbose_name='商品ID', unique=True)
    name = models.CharField(max_length=100, verbose_name='商品名称', null=True, blank=True)
    code = models.CharField(max_length=50, verbose_name='商品编码', unique=True)
    category = models.ForeignKey("GoodsCategory", on_delete=models.CASCADE, verbose_name='商品类型')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格', default=0, db_default='0')
    stock = models.IntegerField(default=0, verbose_name='库存', db_default='0')
    defective_quantity = models.IntegerField(default=0, verbose_name='不良品数量', db_default='0')
    min_stock_alert = models.IntegerField(default=0, verbose_name='最低库存预警', db_default='0')
    description = models.TextField(verbose_name='商品描述', blank=True, null=True)
    color = models.CharField(max_length=30, verbose_name='颜色', null=False, blank=False)
    unit = models.ForeignKey("Units", on_delete=models.CASCADE, verbose_name='单位')
    supplier = models.ForeignKey(Suppliers, on_delete=models.CASCADE)

    class Meta:
        verbose_name = '商品'
        verbose_name_plural = '商品'

    def __str__(self):
        string = "DT" + self.goods_id[2:]
        return f"{string}-{self.name}-{self.code}"


